package com.st.rzs.solr.connection;

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import com.st.rzs.solr.util.ConfigUtil;

/**
 * @desc:solr数据链接
 * @author wangsh
 * @date 2019-01-09 下午8:15:36
 */
public class SolrConncetion {

	// 连接超时时间
	private int conncetionTimeout = ConfigUtil.getInt("config.properties", "solr.connection.timeout.ms", 60000);
	// 最大连接
	private int maxConnectionsPerHost = ConfigUtil.getInt("config.properties", "solr.maxConnectionsPerHost", 100);
	// 最大连接数
	private int maxTotalConnections = ConfigUtil.getInt("config.properties", "solr.maxTotalConnections", 100);

	private String solrUrl = ConfigUtil.getString("config.properties", "solr.url", "http://n.worker.com:8983/solr/");

	private String zookeeper = ConfigUtil.getString("config.properties", "zookeeper.quorum", "");

	/**
	 * 
	 * @Description:获取solr连接
	 * @param url
	 *            ：solr连接url,例如：http://localhost:8090/solr/connection1
	 * @return
	 * @return HttpSolrServer 返回类型
	 * @throws
	 */
	public HttpSolrServer getConnection(String connectionName) {
		HttpSolrServer solrServer = new HttpSolrServer(this.solrUrl + connectionName);
		solrServer.setConnectionTimeout(conncetionTimeout);
		solrServer.setDefaultMaxConnectionsPerHost(maxConnectionsPerHost);
		solrServer.setMaxTotalConnections(maxTotalConnections);
		return solrServer;
	}

	/**
	 * @throws MalformedURLException
	 * 
	 * @Description:获取solr连接
	 * @param url
	 *            ：solr连接url,例如：http://localhost:8090/solr/connection1
	 * @return
	 * @return HttpSolrServer 返回类型
	 * @throws
	 */
	public CloudSolrServer getConnection2(String connectionName) throws MalformedURLException {
		CloudSolrServer solrServer = new CloudSolrServer(zookeeper);
		solrServer.setDefaultCollection(connectionName);
		return solrServer;
	}

	/**
	 * 
	 * @Description:关闭资源
	 * @date:Dec 21, 2017 2:25:13 PM
	 * @throws IOException
	 * @return void 返回类型
	 * @throws
	 */
	public void close() throws IOException {

	}

	public int getConncetionTimeout() {
		return conncetionTimeout;
	}

	public void setConncetionTimeout(int conncetionTimeout) {
		this.conncetionTimeout = conncetionTimeout;
	}

	public int getMaxConnectionsPerHost() {
		return maxConnectionsPerHost;
	}

	public void setMaxConnectionsPerHost(int maxConnectionsPerHost) {
		this.maxConnectionsPerHost = maxConnectionsPerHost;
	}

	public int getMaxTotalConnections() {
		return maxTotalConnections;
	}

	public void setMaxTotalConnections(int maxTotalConnections) {
		this.maxTotalConnections = maxTotalConnections;
	}

	public String getSolrUrl() {
		return solrUrl;
	}

	public void setSolrUrl(String solrUrl) {
		this.solrUrl = solrUrl;
	}

	public String getZookeeper() {
		return zookeeper;
	}

	public void setZookeeper(String zookeeper) {
		this.zookeeper = zookeeper;
	}

}
